home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: Dortmund.Germany.EU.net!gellbo!wolfi
- From: wolfi@gellbo.gellrich-gmbh.de (Actio wolfi)
- Subject: Re: realloc question
- Message-ID: <QG28B81O@gellbo.gellrich-gmbh.de>
- Organization: Gellrich-GmbH
- References: <4ehi4b$qfo@news.texas.net>
- Date: Mon, 29 Jan 1996 19:01:21 GMT
-
- In <4ehi4b$qfo@news.texas.net> gcherer@millenium.texas.net (GT Cherer) writes:
-
- >Dudes-
- >Whether i am on my old 286 with 1 meg, or on my 486 with 8 meg of ram, i
- >get the same result when i do a realloc on about the 50th iteration.( its
- >on an array of pointers). but, when i compile and run the same program on
- >a unix box (with beacoups more memory) it runs with no probs.
-
- >is there a difference in realloc on a dos machine vs unix box?
- >Is there some kind of limiting factor with realloc (other than the amount
- >of ram), like a heap or stack size or something?? i follow the realloc
- >with a calloc for a spot to put a small structure so the two lines look
- >like this, with the realloc returning a NULL:
- >x++;
- >ptr=(struct sumthin**) realloc(ptr,(x+1) * sizeof(struct sumthin));
- >ptr[x]=(struct sumthin*) calloc(1,sizeof(struct sumthin));
-
- >Any ideas?? Sure preeeeeciate 'em. TIA
-
- There should be no difference, except that your UNIX-Box has more memory and
- when the memory gets exhausted it starts to swap !
-
- The problem with realloc is that realloc gives you a pointer to a memory
- area which is as big as you want. When there is no chance to make an
- already allocated memory area larger a new area is used and the contents of
- the previous allocated area is copied to the new area. When your program
- has a sequence of realloc .. malloc .. realloc .. malloc ..., then after a
- while you have used only a part of memory, but *NOT ONE BIG AREA* which
- the realloc can use to satisfy your request. So there are some solutions to
- avoid fragmenting of your memory.
-
- a) Use linked lists. Linked lists need more memory for the pointer stuff
- pointing to the next element, so this is a good solution for bigger
- structures.
- But it needs a lot of rewriting ...
-
-
- b) When you need an extra entry in your realloc'ed array do not call
- realloc with only ony extra element, realloc an estimated extra number
- of elements *AND* at the first time when calling malloc also allocate
- an estimated extra number of elements.
- You just have to add another counter counting the number of elements
- allocated (I think you have a counter counting the number of elements
- used).
-
- c) Rewrite your program doing two passes. The first pass just counts the
- number of elements needed, the second pass reads them in.
-
- I prefer b) for my programs and use a macro like this:
-
- #define BLOCK_ALLOCATE(ptr, typ, no_elements, no_allocated, increment) \
- { \
- if(no_elements >= no_allocated) \
- { \
- if(NULL == (ptr = no_elements ? \
- (typ *)realloc((char *)ptr, \
- (unsigned)(no_allocated += increment) * sizeof(typ)) : \
- (typ *)malloc(sizeof(typ) * (no_allocated = increment)))) \
- abort(); \
- } \
- }
-
- Hope this helps.
-
- Excuse my poor english ;-)
-
- --
- wolfi@gellbo.gellrich-gmbh.de (Wolfgang Formann)
-